package jp.melodroid.no6.testDialog;

import android.app.Activity;
import android.os.Bundle;

import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;

import android.app.Dialog;
import android.app.TimePickerDialog;
import java.util.Calendar;
import android.widget.TimePicker;

/* for Log */
import android.util.Log;

public class TestDialog extends Activity
{
    private Button mPickTime1;
    private Button mPickTime2;

    private TextView mDisplayTime;
    private EditText mEditHour;
    private EditText mEditMinute;

    private int mHour;
    private int mMinute;

    private static final int TIME_DIALOG_ID = 0;
    private static final String CHANGE_DIALOG = "getValue";

    /* for log */
    private static final String TAG ="TestDialog";

    private void logPrint(String s) {
	Log.d(TAG,s);
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

	logPrint("(onCreate):called setContentView");

	mPickTime1 = (Button)findViewById(R.id.pickTime1);
	mPickTime2 = (Button)findViewById(R.id.pickTime2);

	mDisplayTime = (TextView)findViewById(R.id.displayTime);
	mEditHour = (EditText)findViewById(R.id.editHour);
	mEditMinute = (EditText)findViewById(R.id.editMinute);

	mPickTime1.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
		    logPrint("(PickTime1):Clicked");
		    showDialog(TIME_DIALOG_ID);
		}
	    });

	mPickTime2.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
		    logPrint("(PickTime2):Clicked");

		    Bundle b = new Bundle();
		    b.putBoolean(CHANGE_DIALOG,true);
		    showDialog(TIME_DIALOG_ID,b);
		}
	    });

	
	final Calendar c = Calendar.getInstance();
	mHour = c.get(Calendar.HOUR_OF_DAY);
	mMinute = c.get(Calendar.MINUTE);

	updateDisplay();

    }

    private void updateDisplay(){
	logPrint("(updateDisplay):start");

	mDisplayTime.setText(new StringBuilder().append(mHour).
			     append(":").append(mMinute));
    }

    private void updatePrivateValue() {
	logPrint("(updatePrivateValue):start");

	mHour = Integer.valueOf(mEditHour.getText().toString()).intValue();
	mMinute = Integer.valueOf(mEditMinute.getText().toString()).intValue();

	logPrint("(updatePrivateValue):hour:"+ mHour +" min:"+mMinute);
    }

    TimePickerDialog.OnTimeSetListener mTimeSetListener = 
	new TimePickerDialog.OnTimeSetListener() {
	    public void onTimeSet(TimePicker t,int hour,int minute) {
		mHour = hour;
		mMinute = minute;
		updateDisplay();
	    }
	};

    protected Dialog onCreateDialog(int id ,Bundle b) {
	logPrint("(onCreateDialog):start");
	switch(id) {
	case TIME_DIALOG_ID:
	    boolean isChange = false;
	    if (b != null) 
		isChange = b.getBoolean(CHANGE_DIALOG,false);

	    if (isChange)
		updatePrivateValue();
	    return new TimePickerDialog(this, mTimeSetListener,
					mHour,mMinute,false);
	}
	return null;
    }

    protected void onPrepareDialog(int id ,Dialog d,Bundle b) {
	logPrint("(onPrepareDialog):start");

	switch(id) {
	case TIME_DIALOG_ID:
	    boolean isChange = false;
	    if (b != null)
		isChange = b.getBoolean(CHANGE_DIALOG,false);
	    if (isChange)
		updatePrivateValue();
	    if ( d instanceof TimePickerDialog) {
		logPrint("(onPrepareDialog):call updateTime");
		((TimePickerDialog)d).updateTime(mHour,mMinute);
	    }
	}
    }
}
